/*
 * Configuracion.cpp
 *
 *  Created on: 05/05/2011
 *      Author: mmalagamba
 */

#include "Configuration.h"

bool Configuration::instanceFlag = false;
Configuration* Configuration::config = NULL;

Configuration::Configuration() {
	fstream configFile;
		configFile.open("/home/nacho/Desktop/Final/Codigo/configFile.txt", ios::in);

		if (configFile.bad()) {
			configFile.open("/home/nacho/Desktop/Final/Codigo/configFile.txt", ios::in |ios::trunc);
			cout<< "Error: No se encuentra el archivo de configuracion";
			string error;
			cin>>error;
		}
		string carpeta;
		string directorio;
		string linea;
		configFile>>linea;
		string comando;
		if(linea == "directorioPrincipal="){
			configFile>>directorio; //>>endl;
			comando = "mkdir -m 777 -p "+ directorio;
			system(comando.c_str());
		}
		configFile>>linea;
		if(linea == "subcarpetaHash="){
			configFile>>carpeta;
			comando = "mkdir -m 777 -p "+ directorio + carpeta;
			this->setHashPath(directorio + carpeta);
			system(comando.c_str());
		}
		configFile>>linea;
		if(linea == "subcarpetaTree="){
			configFile>>carpeta;
			comando = "mkdir -m 777 -p "+ directorio + carpeta;
			this->setTreePath(directorio + carpeta);
			system(comando.c_str());
		}
		configFile>>linea;
		if(linea == "subcarpetaBooks="){
			configFile>>carpeta;
			comando = "mkdir -m 777 -p "+ directorio + carpeta;
			this->setBookFilePath(directorio + carpeta);
			system(comando.c_str());
		}
		configFile>>linea;
		if(linea == "subcarpetaReports="){
			configFile>>carpeta;
			comando = "mkdir -m 777 -p "+ directorio + carpeta;
			this->setReportsFilesPath(directorio + carpeta);
			system(comando.c_str());
		}else
			this->error();
		configFile>>linea;
		if(linea == "BlockSize="){
			string blockSize;
			configFile>>blockSize;
			this->setBlockSize(atoi(blockSize.c_str()));
		}
	    configFile.close();
}

Configuration* Configuration::getInstance()
{
    if(! instanceFlag)
    {
        config = new Configuration();
        instanceFlag = true;
        return config;
    }
    else
    {
        return config;
    }
}

string Configuration::getBookFilePath() const
{
    return bookFilePath;
}


string Configuration::getHashPath() const
{
    return hashPath;
}

int Configuration::getBlockSize() const
{
    return blockSize;
}

string Configuration::getReportsFilesPath() const
{
    return reportsFilesPath;
}

void Configuration::setBlockSize(int blockSize)
{
    this->blockSize = blockSize;
}

void Configuration::setReportsFilesPath(string reportsFilesPath)
{
    this->reportsFilesPath = reportsFilesPath;
}

string Configuration::getTreePath() const
{
    return treePath;
}

void Configuration::setBookFilePath(string bookFilePath)
{
    this->bookFilePath = bookFilePath;
}

void Configuration::setHashPath(string hashPath)
{
    this->hashPath = hashPath;
}

void Configuration::setTreePath(string treePath)
{
    this->treePath = treePath;
}
void Configuration::error(){

}
Configuration::~Configuration() {
 instanceFlag = false;
}
